package com.itheima.case2.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.CaseServcie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/CaseServlet/*")
public class CaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{

            String requestURI = request.getRequestURI();
            int i = requestURI.lastIndexOf("/");
            String substring = requestURI.substring(i + 1,requestURI.length());
            Method method = CaseServlet.class.getMethod(substring, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void findAllByPage(HttpServletRequest request,HttpServletResponse response) throws Exception{
        try {
//            接受数据并把json转换成对象
            QueryPageBean queryPageBean = JSON.parseObject(request.getInputStream(), QueryPageBean.class);
            CaseServcie servcie=new CaseServcie();
            response.setContentType("application/json;charset=utf-8");
            PageResult allByPage = servcie.findAllByPage(queryPageBean);
            Result result=new Result(true,"查询成功",allByPage);
            JSON.writeJSONString(response.getWriter(),result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询角色
     * @param request
     * @param response
     * @throws Exception
     */
    public void findRoles(HttpServletRequest request,HttpServletResponse response) throws Exception{
        CaseServcie servcie=new CaseServcie();
        List<Role> roles = servcie.findRoles();
        response.setContentType("application/json;charset=utf-8");
        Result result=new Result(true,"查询成功",roles);
        JSON.writeJSONString(response.getWriter(),result);
    }

    public void addRoleUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.setContentType("application/json;charset=utf-8");
        AddUser addUser = JSON.parseObject(request.getInputStream(), AddUser.class);
        CaseServcie servcie=new CaseServcie();
        servcie.addRoleUser(addUser);
        Result result=new Result(true,"插入成功");
        JSON.writeJSONString(response.getWriter(),result);
    }


    public void update(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.setContentType("application/json;charset=utf-8");
        UpdateUser updateUser = JSON.parseObject(request.getInputStream(), UpdateUser.class);
        CaseServcie servcie=new CaseServcie();
        servcie.update(updateUser);
        Result result=new Result(true,"修改成功");
        JSON.writeJSONString(response.getWriter(),result);
    }

    public void delete(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.setContentType("application/json;charset=utf-8");
        String row = request.getParameter("row");
//        用户id
        int i = Integer.parseInt(row);
        CaseServcie servcie=new CaseServcie();
        servcie.delete(i);
        Result result=new Result(true,"删除成功");
        JSON.writeJSONString(response.getWriter(),result);

    }
}
